﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSunny.Admin.Controls
{
    public partial class ViewDetailVendor : System.Web.UI.UserControl
    {
        public string vendorname;
        public string vendorlogo;
        public string vendordescription;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["idvendor"] != null)
            {
                int vendorid = Convert.ToInt32(Request.QueryString["idvendor"]);
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Vendor", vendorid.ToString());
                vendorname = dt.Rows[0]["Vendor_Name"].ToString();
                vendorlogo = dt.Rows[0]["Vendor_LoGo"].ToString();
                vendordescription = dt.Rows[0]["Vendor_Description"].ToString();

                txtVendorName.Text = vendorname;
                txtVendorDescription.Text = vendordescription;
            }
        }

        protected void btnUpdateVendor_Click(object sender, EventArgs e)
        {
            int vendorid = Convert.ToInt32(Request.QueryString["idvendor"]);

            string name = txtVendorName.Text;
            string descriptionvendor = txtVendorDescription.Text;
            string filePathVendor = null;
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Vendor", vendorid.ToString());
            vendorlogo = dt.Rows[0]["Vendor_LoGo"].ToString();

            if (name != "")
            {
                if (fulImage.HasFile)
                {
                    if (LIB.Validate.CheckFileType(fulImage.FileName))
                    {
                        string link = fulImage.FileName;
                        LIB.Validate saveimage = new LIB.Validate();
                        saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Images/Image-Vendor/"), link, 250, fulImage.PostedFile.InputStream);
                        filePathVendor = "/Images/Image-Vendor/" + fulImage.FileName;
                        if (vendorlogo == "")
                        {
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[3];
                            cl[0] = new DAL.DHelpers.Column("Vendor_Name", name, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[1] = new DAL.DHelpers.Column("Vendor_LoGo", filePathVendor, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[2] = new DAL.DHelpers.Column("Vendor_Description", descriptionvendor, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                            string colWhere = "Vendor_ID";

                            string colValue = vendorid.ToString();

                            DAL.DCollector.DBCollector.Instance.StoredUpdate("tbl_Vendor", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=1&sub=2");
                        }
                        else
                        {
                            File.Delete(MapPath(vendorlogo));
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[3];
                            cl[0] = new DAL.DHelpers.Column("Vendor_Name", name, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[1] = new DAL.DHelpers.Column("Vendor_LoGo", filePathVendor, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[2] = new DAL.DHelpers.Column("Vendor_Description", descriptionvendor, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                            string colWhere = "Vendor_ID";

                            string colValue =  vendorid.ToString();

                            DAL.DCollector.DBCollector.Instance.StoredUpdate("tbl_Vendor", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=1&sub=2");
                        }

                    }
                    else
                    {
                        Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                    }
                }

                else
                {
                    DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[2];
                    cl[0] = new DAL.DHelpers.Column("Vendor_Name", name, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[1] = new DAL.DHelpers.Column("Vendor_Description", descriptionvendor, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                    string colWhere = "Vendor_ID";

                    string colValue = vendorid.ToString();

                    DAL.DCollector.DBCollector.Instance.StoredUpdate("tbl_Vendor", cl, colWhere, colValue);
                    Response.Redirect("/Admin/Index.aspx?panel=1&sub=2");
                }
            }
            else
            {
                Response.Write("<script>alert('Vui lòng nhập tên danh mục...')</script>");
            }
        }

        protected void lbtDelete_Click(object sender, EventArgs e)
        {
            int vendorid = Convert.ToInt32(Request.QueryString["idvendor"]);
            DAL.DCollector.DBCollector.Instance.StoredDelete("tbl_Vendor", "Vendor_ID", vendorid.ToString());
            Response.Redirect("/Admin/Index.aspx?panel=1&sub=2");
        }
    }
}